<html>
<head><meta charset="utf-8"><title>generic vs #[inline] · t-compiler/performance · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/index.html">t-compiler/performance</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/generic.20vs.20.23.5Binline.5D.html">generic vs #[inline]</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="205643363"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/generic%20vs%20%23%5Binline%5D/near/205643363" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> andjo403 <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/generic.20vs.20.23.5Binline.5D.html#205643363">(Jul 31 2020 at 21:26)</a>:</h4>
<p>have seen multiple threads references to that a function do not need #[inline] as the function is generic  but I do not understand what the connection between them is.<br>
I assume that not all generic functions is marked with inline hint in llvm-ir, altering how willing llvm is to inline so a more complex function may get inlined with the hint. <br>
is there some confusion about generic functions is generated in the crate that uses it? but even then it is generated in several CGUs so is not possible to assume that they are inlined due to that.<br>
or have I missed something?</p>



<a name="205644070"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/generic%20vs%20%23%5Binline%5D/near/205644070" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/generic.20vs.20.23.5Binline.5D.html#205644070">(Jul 31 2020 at 21:35)</a>:</h4>
<p><code>#[inline]</code> has two effects in rust today: it makes the function available for inlining (by codegen'ing it into any CGU that could use it) and adds the LLVM inlinehint attribute.</p>
<p>Generics already take care of the first, just by their nature (at least without -Zshare-generics, I believe), which means that you're only adding the inlinehint attribute. A lot of the time that's not necessary -- LLVM is already going to make the right call, so it may be best to not touch things</p>



<a name="205644186"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/generic%20vs%20%23%5Binline%5D/near/205644186" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/generic.20vs.20.23.5Binline.5D.html#205644186">(Jul 31 2020 at 21:36)</a>:</h4>
<p>not sure CGUs are the most relevant part. without <code>#[inline]</code>, the MIR of the function is just not stored anywhere, and it's only codegen'd once, as part of the original crate</p>



<a name="205644286"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/generic%20vs%20%23%5Binline%5D/near/205644286" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/generic.20vs.20.23.5Binline.5D.html#205644286">(Jul 31 2020 at 21:37)</a>:</h4>
<p>Within a crate, though, the CGU bit is correct, right?</p>



<a name="205644519"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/generic%20vs%20%23%5Binline%5D/near/205644519" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> andjo403 <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/generic.20vs.20.23.5Binline.5D.html#205644519">(Jul 31 2020 at 21:41)</a>:</h4>
<p>I'm almost sure that  a generic function it is not in all the CGUs  in the using crate, only in one  CGUper crate</p>



<a name="205644646"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/generic%20vs%20%23%5Binline%5D/near/205644646" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/generic.20vs.20.23.5Binline.5D.html#205644646">(Jul 31 2020 at 21:42)</a>:</h4>
<p><span class="user-mention" data-user-id="116122">@simulacrum</span> maybe, I have no idea, but AFAIK it's not where the drastic differences come from</p>



<a name="205645099"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/generic%20vs%20%23%5Binline%5D/near/205645099" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> andjo403 <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/generic.20vs.20.23.5Binline.5D.html#205645099">(Jul 31 2020 at 21:47)</a>:</h4>
<p>ups a not was gone in my message not in all CGUs  was what I wanted to write</p>



<a name="205645533"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/generic%20vs%20%23%5Binline%5D/near/205645533" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> andjo403 <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/generic.20vs.20.23.5Binline.5D.html#205645533">(Jul 31 2020 at 21:52)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/blob/47ea6d90b073ab977cf072e2f5f46d63de532cc6/src/librustc_mir/monomorphize/partitioning.rs#L42">https://github.com/rust-lang/rust/blob/47ea6d90b073ab977cf072e2f5f46d63de532cc6/src/librustc_mir/monomorphize/partitioning.rs#L42</a> states <br>
//! - There are two codegen units for every source-level module:<br>
//! - One for "stable", that is non-generic, code<br>
//! - One for more "volatile" code, i.e., monomorphized instances of functions<br>
//!   defined in that module</p>
<p>so all generic functions from one module is places in one CGU</p>



<a name="205645825"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/generic%20vs%20%23%5Binline%5D/near/205645825" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> andjo403 <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/generic.20vs.20.23.5Binline.5D.html#205645825">(Jul 31 2020 at 21:55)</a>:</h4>
<p>that is why if  there is a lot  of generic code in a module and generic over many types one CGU can be huge compared to all the other CGUs</p>



<a name="205645888"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/generic%20vs%20%23%5Binline%5D/near/205645888" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/generic.20vs.20.23.5Binline.5D.html#205645888">(Jul 31 2020 at 21:56)</a>:</h4>
<p>I think that doesn't quite describe the full picture -- notably, there's no mention there about functions <em>not</em> defined in that module</p>



<a name="205645941"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/generic%20vs%20%23%5Binline%5D/near/205645941" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/generic.20vs.20.23.5Binline.5D.html#205645941">(Jul 31 2020 at 21:57)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/blob/47ea6d90b073ab977cf072e2f5f46d63de532cc6/src/librustc_mir/monomorphize/partitioning.rs#L83">https://github.com/rust-lang/rust/blob/47ea6d90b073ab977cf072e2f5f46d63de532cc6/src/librustc_mir/monomorphize/partitioning.rs#L83</a> seems wrong, I guess, though I could be misinterpreting</p>



<a name="205646075"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/generic%20vs%20%23%5Binline%5D/near/205646075" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/generic.20vs.20.23.5Binline.5D.html#205646075">(Jul 31 2020 at 21:58)</a>:</h4>
<p><span class="user-mention" data-user-id="125799">@andjo403</span> <code>#[inline]</code> does double-duty as "this function is serialized and can be codegen'd in another crate" and "inline hint". this where the "generic functions don't need <code>#[inline]</code>" comes from, and it predates the existence of CGUs (or rather, more than one CGU per crate)</p>



<a name="205646105"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/generic%20vs%20%23%5Binline%5D/near/205646105" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/generic.20vs.20.23.5Binline.5D.html#205646105">(Jul 31 2020 at 21:58)</a>:</h4>
<p>sure, nowadays the whole picture is more complicated, but the origin is simpler</p>



<a name="205646148"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/generic%20vs%20%23%5Binline%5D/near/205646148" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/generic.20vs.20.23.5Binline.5D.html#205646148">(Jul 31 2020 at 21:59)</a>:</h4>
<p>Should we break it up?</p>



<a name="205646175"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/generic%20vs%20%23%5Binline%5D/near/205646175" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/generic.20vs.20.23.5Binline.5D.html#205646175">(Jul 31 2020 at 21:59)</a>:</h4>
<p>generic functions are always serialized because there's no way around it, so for them <code>#[inline]</code> is <em>just</em> an inline hint, which is less useful/necessary (again, ignoring any modern CGU shenanigans)</p>



<a name="205646232"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/generic%20vs%20%23%5Binline%5D/near/205646232" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/generic.20vs.20.23.5Binline.5D.html#205646232">(Jul 31 2020 at 22:00)</a>:</h4>
<p><span class="user-mention" data-user-id="224471">@Lokathor</span> <em>shrug</em>, if you search around you should find previous discussions, it's not like it was never thought of</p>



<a name="205646252"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/generic%20vs%20%23%5Binline%5D/near/205646252" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/generic.20vs.20.23.5Binline.5D.html#205646252">(Jul 31 2020 at 22:00)</a>:</h4>
<p>but I don't remember the details</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>